include('shared.lua')

function ENT:Initialize()
    self.cl_repair = 1
    self.cl_driver = NULL
    
    self.cycle = 0
    self.CalcTime = CurTime()
    self.ManipTime = CurTime()
    
    self.radius = self.Entity:BoundingRadius()
    
    local max = self.Entity:OBBMaxs()
    local min = self.Entity:OBBMins()
    local width = math.abs(max.x-min.x)
    local height = math.abs(max.y-min.y)
    local depth = math.abs(max.z-min.z)
    
    if(width<height and width<depth) then
        self.Ang = Angle(0,0,1)
    elseif(height<width and height<depth) then
        self.Ang = Angle(1,0,0)
    else
        self.Ang = Angle(0,1,0)
    end
    
    if(self.Entity:GetModel()=="models/props_phx/misc/propeller2x_small.mdl") then self.Ang = Angle(0,1,0) end
    
    local effectdata = EffectData()
    effectdata:SetEntity( self.Entity )
    util.Effect( "st_cl_fan", effectdata )
end

function ENT:Think()
     if(self.CalcTime<CurTime()) then
        
        self.cl_driver = self.Entity:GetNetworkedEntity("driver")
        self.cl_repair = self.Entity:GetNetworkedFloat("repair")
        local cl_thrust = self.Entity:GetNetworkedFloat("thrust")*16
        
        local prevcycle = self.cycle
        self.cycle = self.cycle+ (cl_thrust*2/math.pow(self.radius,0.3))
        if(self.cycle>360) then self.cycle = self.cycle-360 elseif(self.cycle<0) then self.cycle = self.cycle+360 end
        
        if(self.ManipTime<CurTime()) then
            if(prevcycle==self.cycle) then /*comment*/ else
                if(self.effect and self.effect:IsValid() and (self.effect:GetPos()-self.Entity:GetPos()):Length() < 16) then
                    self.effect:SetAngles(self.Entity:LocalToWorldAngles( Angle(-self.cycle*self.Ang.p,-self.cycle*self.Ang.y,-self.cycle*self.Ang.r) ))
                else
                    local effectdata = EffectData()
                    effectdata:SetEntity( self.Entity )
                    util.Effect( "st_cl_fan", effectdata )
                end
            end
            local dist = (LocalPlayer():GetShootPos() - self.Entity:GetPos()):Length()
            self.ManipTime = CurTime()+math.Clamp( 0.01 + math.floor(dist/1000)/100 ,0,1)
        end
        
        self.CalcTime = CurTime()+0.01
    end
    
    if(self.cl_repair<1 and self.cl_driver==LocalPlayer()) then
        local pos = self.Entity:LocalToWorld(Vector(0,0,self.Entity:OBBCenter().z))
        repairhud_info[self.Entity:EntIndex()] = {vec = pos,val=self.cl_repair}
    end
end

function ENT:Draw()
	//self:DrawModel()
end